﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace BO
{
    /// <summary>
    /// Summary description for StatisticBO
    /// </summary>
    public class StatisticBO : DBConnectionBO
    {
        public StatisticBO()
            : base()
        {

        }

        /// <summary>
        /// Get list statistic
        /// </summary>
        /// <returns></returns>
        public List<Statistic> GetListStatistic()
        {
            var listStatistic = new List<Statistic>();
            const string query = "SELECT TOP 100 * FROM [PrivateBlog].[dbo].[Statistics]";
            var sqlDataAdapter = new SqlDataAdapter(query, connection);
            var dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable); // fill data to Datatable
            this.CloseConnection(); // Close connection to database
            for (var i = 0; i < dataTable.Rows.Count; i++)
            {
                var statistic = new Statistic
                    {
                        Id = (int)dataTable.Rows[i][0],
                        Name = (string)dataTable.Rows[i][1],
                        Description = (string)dataTable.Rows[i][2],
                        Actor = (string)dataTable.Rows[i][3],
                        LastDateUpdated = (DateTime)dataTable.Rows[i][4],
                        LastUpdatedBy = (string)dataTable.Rows[i][5],
                        Status = Convert.ToInt32(dataTable.Rows[i][6])
                    };
                listStatistic.Add(statistic);
            }
            return listStatistic;
        }

        public int AddStatistic(Statistic statistic)
        {
            var query = "INSERT INTO [PrivateBlog].[dbo].[Statistics]([Name],[Description],[Actor],[LastDateUpdated]," +
                        "[LastUpdatedBy],[Status]) VALUES ( "
                + "N'" + statistic.Name + "', N'" +
                statistic.Description + "', N'" +
                statistic.Actor + "', GETDATE(), N'" +
                statistic.LastUpdatedBy + "', " +
                statistic.Status +
                " ) ";
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Edit statistic
        /// </summary>
        /// <param name="statistic"></param>
        /// <returns></returns>
        public int EditStatistic(Statistic statistic)
        {
            var query = "UPDATE [PrivateBlog].[dbo].[Statistics]" +
                        "SET Name = N'" + statistic.Name + "', " +
                        "Description = N'" + statistic.Description + "', " +
                        "Actor = N'" + statistic.Actor + "', " +
                        "LastDateUpdated = GETDATE(), " +
                        "LastUpdatedBy = N'" + statistic.LastUpdatedBy + "', " +
                        "Status = " + statistic.Status + ", " +
                        "WHERE Id = " + statistic.Id;
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Delete statistic
        /// </summary>
        /// <param name="statistic"></param>
        /// <returns></returns>
        public int DeleteStatistic(Statistic statistic)
        {
            var query = "DELETE FROM [PrivateBlog].[dbo].[Events] " +
                        " WHERE Id = " + statistic.Id;
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }
    }
}